#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100000
void bubble_sort(int *, int);

int main(int argc, const char **argv) {
  srand(time(NULL));
  clock_t start, end;
  double cpu_time_used;

  start = clock(); // 记录开始时间

  int *buf;
  buf = malloc(sizeof(int) * N);
  for (int i = 0; i < N; i++)
    buf[i] = rand() % N;

  bubble_sort(buf, N);

  end = clock(); // 记录结束时间

  cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC; // 计算CPU时间
  printf("程序运行时间为 %f 秒\n", cpu_time_used);
  return 0;
}

void bubble_sort(int *buf, int num) {
  int tmp;
  for (int i = 0; i < num - 1; i++) {
    for (int j = 0; j < num - 1 - i; j++) {
      tmp = buf[j];
      buf[j] = buf[j + 1];
      buf[j + 1] = tmp;
    }
  }
}
